<?php
namespace app\admin\controller;
use think\Controller;

Class CategoryBrands extends BaseAdmin
{

	//关联品牌列表
    public function lst()
    {
    	
        
        $CategoryBrandsRes=db('tp_category_brands')->alias('a')->field('a.*,b.cate_name')->join('tp_category b','a.category_id=b.id')->paginate(5);
         //获取品牌信息
        $BrandsRes=db('tp_brand')->select();
        // dump($BrandsRes);die;
        $this->assign([
            'CategoryBrandsRes'=>$CategoryBrandsRes,
            'BrandsRes'=>$BrandsRes
        ]);

        return $this->fetch('list');
    }

    //关联品牌添加
    public function add()
    {

        //顶级栏目信息
        $TopCates=db('tp_category')->where(['pid'=>0])->select();

        //获取品牌信息
        $BrandsRes=db('tp_brand')->select();

        $this->assign([
            'TopCates'=>$TopCates,
            'BrandsRes'=>$BrandsRes
        ]);
    	 
        if(request()->isPost()){
        	$data=input('post.');
            // dump($data);die;
        	if($data['pro_url']&&stripos($data['pro_url'],'http://')===false){
        		$data['pro_url']='http://'.$data['pro_url'];
        	}

        	if($_FILES['pro_img']['tmp_name']){
        		$data['pro_img']=$this->upload();
        	}
            if(isset($data['brands_id'])){
                $data['brands_id']=implode(',',$data['brands_id']);
            }


        	//验证
        	// $validate = new \app\admin\validate\CategoryBrands;
        	// if (!$validate->check($data)) {
        	// 	$this->error($validate->getError());
        	// }
        	
        	$add=db('tp_category_brands')->insert($data);
        	if($add){
        		$this->success('保存成功','lst');
        	}
        	else{
        		$this->error('保存失败');
        	}

        }
        return $this->fetch();

    }

    //关联品牌编辑
    public function edit($id)
    {
    	$CategoryBrandsRes=db('tp_category_brands')->where(array('id'=>$id))->find();

        //顶级栏目信息
        $TopCates=db('tp_category')->where(['pid'=>0])->select();

        //获取品牌信息
        $BrandsRes=db('tp_brand')->select();
    	$this->assign([
            'CategoryBrandsRes'=>$CategoryBrandsRes,
            'TopCates'=>$TopCates,
            'BrandsRes'=>$BrandsRes
        ]);
    	if(request()->isPost()){
        	$data=input('post.');
            
        	if($data['pro_url']&&stripos($data['pro_url'],'http://')===false){
        		$data['pro_url']='http://'.$data['pro_url'];
        	}

        	if($_FILES['pro_img']['tmp_name']){
                $img=IMG_UPLOADS.$CategoryBrandsRes['pro_img'];
                if(file_exists($img)){
                    @unlink($img);
                }
        		$data['pro_img']=$this->upload();
        	}
            if(isset($data['brands_id'])){
                $data['brands_id']=implode(',',$data['brands_id']);
            }
      
        	$up=db('tp_category_brands')->where(array('id'=>$id))->update($data);
        	
        	if($up!==false){
        		$this->success('保存成功','lst');
        	}
        	else{
        		$this->error('保存失败');
        	}

        }
        return $this->fetch();
    }

    //关联品牌删除
    public function del($id)
    {
    	$del=db('tp_category_brands')->delete($id);
    	if($del){
    		$this->success('删除成功','lst');

    	}else{
            $this->error('删除失败');
    	}
    	
        return $this->fetch();
    }

    
    //图片上传
	public function upload(){
		$file=request()->file('pro_img');
		if($file==null){
			exit(json_encode(array('msg'=>'没有文件上传')));
		}
		$info=$file->move('../public/static/uploads');
		$ext=$info->getExtension();//获取后缀名
		if(!in_array($ext,array('jpg','png','gif','jpeg'))){
			exit(json_encode(array('msg'=>'文件格式不符合')));
		}
		if($info){
			return $img=$info->getSaveName();

		}else{
			$file->getError();
			die;
		}
		
		
	}


}